/*
 * @Author: Scrooge yu844856744@qq.com
 * @Date: 2023-04-10 14:51:39
 * @LastEditors: Scrooge yu844856744@qq.com
 * @LastEditTime: 2023-05-12 17:40:01
 * @FilePath: /seehoo-design-plus/src/components/sh-table/utils/useAction.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { computed } from 'vue';
import { useType } from '../../../utils/useType';

const { isType } = useType();

export const useAction = props => {
  /**
   * @desciption 计算属性为显示的操作列
   * @returns {Object} actions
   */
  const getActions = computed(() => {
    return props.actions.filter(item => {
      // return item.show ? (isType(item.show, 'Function') ? item.show() : item.show) : true;
      if (!item.hasOwnProperty('type')) {
        item.type = 'text';
      }
      if (!item.hasOwnProperty('show')) {
        item.show = true;
      }
      if (isType(item.show, 'Function')) {
        return item.show();
      }
      return item.show;
    });
  });

  /**
   * @desciption 计算合并bind对象
   * @param {Object} attrs
   * @returns {Object} bindAttrs
   */
  const getBindValues = computed(() => {
    return (attrs, item) => {
      let propsData = Object.assign(
        {},
        {
          ...attrs,
          ...item,
        }
      );
      delete propsData['onClick'];
      return propsData;
    };
  });

  return {
    getActions,
    getBindValues,
  };
};
